﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using DBMS.Data;
using DBMS.Models;
using DBMS.Models.DataInterfaces;
using DBMS.Helpers.Metadata;
using DBMS.Indexes;
using DBMS.Indexes.Tree.BPlus;

namespace DBMS.Controllers
{
    public class DatabasesController : Controller
    {
        private IDatabaseRepository databaseRepository;
        private IRelationshipRepository relationshipRepository;

        public DatabasesController()
        {
            //temporary - repository injection in consideration
            this.databaseRepository = new DatabaseRepository();
            this.relationshipRepository = new RelationshipRepository();
        }

        public ActionResult Index(string databaseName)
        {
            IList<Database> databases = databaseRepository.GetAll();

            ViewBag.Databases = databases;
            //Tree<int,  String> bPlusTree = new Tree<int, String>(2, "..");
            //bPlusTree.Insert(new ComparableInteger(2), "2");
            //bPlusTree.Insert(new ComparableInteger(5), "5");
            //bPlusTree.Insert(new ComparableInteger(9), "9");
            //bPlusTree.Insert(new ComparableInteger(3), "3");
            //bPlusTree.Insert(new ComparableInteger(1), "1");
            //bPlusTree.Insert(new ComparableInteger(11), "11");

            //ViewBag.Tree = bPlusTree;

            return View();
        }

        public ActionResult CreateDatabase()
        {
            return PartialView();
        }

        public ActionResult Relationships(string databaseId)
        {
            IList<Relationship> relationships = relationshipRepository.GetAllRelationshipsForDatabase(Int32.Parse(databaseId));

            ViewBag.Relationships = relationships;
            ViewBag.DatabaseId = databaseId;

            return View();
        }

        public ActionResult DeleteRelationship(string databaseId, string relId)
        {
            relationshipRepository.Delete(Int32.Parse(databaseId), Int32.Parse(relId));

            return RedirectToAction("Relationships", new { databaseId = databaseId });
        }

        public ActionResult EditDatabase(string databaseId)
        {
            Database database = databaseRepository.GetDatabase(Int32.Parse(databaseId));

            return PartialView(database);
        }

        [HttpPost]
        public ActionResult EditDatabase(Database database)
        {
            try
            {
                databaseRepository.Update(database);
            }
            catch
            {

            }

            return RedirectToAction("Index", "Tables", new { databaseId = database.Index, databaseName = database.Name });
        }


        [HttpPost]
        public ActionResult CreateDatabase(Database database)
        {
            try
            {
                databaseRepository.CreateDatabase(database);
            }
            catch
            {
            }
            return RedirectToAction("Index", "Tables", new { databaseId = database.Index, databaseName = database.Name });
        }

        public ActionResult Menu()
        {
            IList<Database> databases = databaseRepository.GetAll();

            ViewBag.Databases = databases;

            return PartialView();
        }

    }
}
